""" 排序加双指针，注意去重部分的指针迭代 """
from typing import * 
def threeSum(self, nums: List[int]) -> List[List[int]]:
        
    n = len(nums)
    if not nums or n<3:
        return []

    nums.sort()
    res = []
    for i in range(n):
        if nums[i]>0:
            return res
        if i > 0 and nums[i] == nums[i-1]:
            continue

        left, right = i+1, n-1

        while left < right:
            if nums[i]+nums[left]+nums[right] == 0:
                res.append([nums[i],nums[left],nums[right]])
                while left<right and nums[left] == nums[left+1]:
                    left += 1
                while left<right and nums[right] == nums[right-1]:
                    right -= 1
                # 更新下标
                left += 1
                right -= 1

            elif nums[i]+nums[left]+nums[right] > 0:
                right -= 1
            else:
                left += 1
    return res 


print(threeSum([-1,0,1,2,-1,-4]))
